
import 'package:canvas/common_utils/utils.dart';
import 'package:flutter/material.dart';

class DrawLoveWidget extends StatefulWidget {
  const DrawLoveWidget({super.key});

  @override
  State<DrawLoveWidget> createState() => _DrawLoveWidgetState();
}

class _DrawLoveWidgetState extends State<DrawLoveWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("画爱心"),
      ),
      body: Center(
        child: Container(
          width: 300,
          height: 400,
          color: Colors.yellow,
          child: CustomPaint(
            painter: DrawLove(),
          ),
        ),
      ),
    );
  }
}

class DrawLove extends CustomPainter{
  @override
  void paint(Canvas canvas, Size size) {

    var p0 = Offset(size.width*0.2/5, size.height/3);
    var p1 = Offset(size.width*2.5/5, size.height/3);
    var p01 = Offset(size.width*1.2/5, 0);
    var p2 = Offset(size.width*4.8/5, size.height/3);
    var p12 = Offset(size.width*3.8/5, 0);
    var p3 = Offset(size.width*2.5/5, size.height*6/7);
    var p30 = Offset(size.width*0/5, size.height*1.4/3);
    var p32 =  Offset(size.width*5/5, size.height*1.4/3);

    Paint paint = Paint()
    ..color = Colors.red
    ..strokeWidth = 1.0
    ..strokeCap = StrokeCap.round;

    Path path = Path();
    path.moveTo(p0.dx, p0.dy);
    for(var t = 0;t<100;t++){
      Offset offset = get2OrderBezierPoint(p0, p01, p1, t/100);
      path.lineTo(offset.dx, offset.dy);
    }
    for(var t = 0;t<100;t++){
      Offset offset = get2OrderBezierPoint(p1, p12, p2, t/100);
      path.lineTo(offset.dx, offset.dy);
    }
    for(var t = 0;t<100;t++){
      Offset offset = get2OrderBezierPoint(p2, p32, p3, t/100);
      path.lineTo(offset.dx, offset.dy);
    }
    for(var t = 0;t<100;t++){
      Offset offset = get2OrderBezierPoint(p3, p30, p0, t/100);
      path.lineTo(offset.dx, offset.dy);
    }
    canvas.drawPath(path, paint);

  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    throw true;
  }

}